package com.samoyed.credit;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * gif 动画加载
 */

public class GifActivity extends Activity {
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_gif);
        image = (ImageView) findViewById(R.id.gifImage);
        load(image);

    }

    private void load(ImageView image) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                //.placeholder(R.mipmap.ic_launcher_round)
                .error(android.R.drawable.stat_notify_error)
                .priority(Priority.HIGH)
                //.skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        Glide.with(this)
                .load(R.drawable.example)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (drawable instanceof GifDrawable) {
                            GifDrawable gifDrawable = (GifDrawable) drawable;
                            gifDrawable.setLoopCount(1);
                            image.setImageDrawable(drawable);
                            gifDrawable.start();
                            Field gifStateField = null;
                            try {
                                gifStateField = GifDrawable.class.getDeclaredField("state");
                                gifStateField.setAccessible(true);
                                Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
                                Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader");
                                gifFrameLoaderField.setAccessible(true);
                                Class gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
                                Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder");
                                gifDecoderField.setAccessible(true);
                                Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder");
                                Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(gifDrawable)));
                                Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class);
                                getDelayMethod.setAccessible(true);
                                //获得总帧数
                                int count = gifDrawable.getFrameCount();
                                int delay = 0;
                                for (int i = 0; i < count; i++) {
                                    //计算每一帧所需要的时间进行累加
                                    delay += (int) getDelayMethod.invoke(gifDecoder, i);
                                }
                                image.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Log.e("longke", "播放完成");
                                        finish();
                                    }
                                }, delay);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        return false;
                    }
                })
                .apply(options)
                //.thumbnail(Glide.with(this).load(R.mipmap.ic_launcher))
                .into(image);
    }


}